package com.lx.core.config;

import com.lx.Response;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * Created by lanyxp on 2021/2/23 10:46
 */
@Configuration
@ConditionalOnProperty(prefix = "lx.swagger", name = "enabled", havingValue = "true")
public class SwaggerAutoConfig {
    private static final String BASE_PACKAGES = "com.lx";
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .apiInfo(buid())
                .select()
                .apis(RequestHandlerSelectors.basePackage(BASE_PACKAGES))
                .paths(PathSelectors.any()).build()
                .directModelSubstitute(LocalDate.class, String.class)
                .directModelSubstitute(LocalDateTime.class, String.class)
                .directModelSubstitute(LocalTime.class, String.class)
                .genericModelSubstitutes(Response.class);
    }

    private ApiInfo buid() {
        return new ApiInfoBuilder()
                .title("LX RESTFUL API")
                .description("在线api")
                .version("1.0.0")
                .build();
    }
}
